package com.yy.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.yy.model.SysFileuploadEntity;
import com.yy.service.SysFileuploadService;
import com.yy.utils.PageUtils;
import com.yy.utils.PropertiesUtil;
import com.yy.utils.Query;
import com.yy.utils.ResponseEntity;
import com.yy.utils.file.FileUploadUtil;


/**
 * 本地文件上传
 * 
 * @author yy
 * @email 
 * @date 2017-07-18 17:28:48
 */
@RestController
@RequestMapping("sysfileupload")
public class SysFileuploadController {
	@Autowired
	private SysFileuploadService sysFileuploadService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sysfileupload:list")
	public ResponseEntity list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<SysFileuploadEntity> sysFileuploadList = sysFileuploadService.queryList(query);
		int total = sysFileuploadService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(sysFileuploadList, total, query.getLimit(), query.getPage());
		
		return ResponseEntity.ok().put("page", pageUtil);
	}
	
	/**
	 * 图片上传-改名-按时间储存-接口
	 * @param file 文件
	 * @param fileName 文件名 
	 * @param dirName 文件储存目录
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/imageUpload")
	public ResponseEntity imageUpload(@RequestParam(value="imageFile",required=false)MultipartFile imageFile,
			@RequestParam(name="fileName",required=false)String fileName,@RequestParam(name="dirName",required=false)String dirName){
		if(StringUtils.isBlank(dirName)){
			return  ResponseEntity.error("上传目录为空");
		}
		
		//图片名
		String imageName = FileUploadUtil.reportImgName(imageFile, dirName, fileName);
		if(imageName==null){
			return  ResponseEntity.error("上传失败");
		}
		//图片远程路径
		String imgstr = String.format("%s%s%s",PropertiesUtil.getImgHttpAddress(),dirName,"/");
		String imagestr=String.format("%s%s%s",new SimpleDateFormat("yyyy/MM").format(System.currentTimeMillis()),"/",imageName);
		String imgPath = imgstr+imagestr;
		
		SysFileuploadEntity sysFileupload =new SysFileuploadEntity();
		sysFileupload.setUrl(imgPath);
		sysFileupload.setCreateDate(new Date());
		sysFileuploadService.save(sysFileupload);
		return  ResponseEntity.response(imagestr);
	}
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sysfileupload:info")
	public ResponseEntity info(@PathVariable("id") Long id){
		SysFileuploadEntity sysFileupload = sysFileuploadService.queryObject(id);
		
		return ResponseEntity.ok().put("sysFileupload", sysFileupload);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sysfileupload:save")
	public ResponseEntity save(@RequestBody SysFileuploadEntity sysFileupload){
		sysFileuploadService.save(sysFileupload);
		
		return ResponseEntity.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sysfileupload:update")
	public ResponseEntity update(@RequestBody SysFileuploadEntity sysFileupload){
		sysFileuploadService.update(sysFileupload);
		
		return ResponseEntity.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sysfileupload:delete")
	public ResponseEntity delete(@RequestBody Long[] ids){
		for (Long l : ids) {
			SysFileuploadEntity sysFileupload =sysFileuploadService.queryObject(l);
			String fileName = sysFileupload.getUrl().replaceAll(PropertiesUtil.getImgHttpAddress(), "");
			String oldFilePath = String.format("%s%s", PropertiesUtil.getImgLocalPath(),fileName);
			System.out.println(oldFilePath);
			File old = new File(oldFilePath);
			old.delete();
		}
		sysFileuploadService.deleteBatch(ids);
		
		return ResponseEntity.ok();
	}
	
}
